package com.example.image;

import android.graphics.Bitmap;
import android.util.LruCache;

import com.example.image.utils.Md5Encryption;

public class PicLru {

    private static PicLru instance;

    private PicLru() {
        initLru();
    }

    public synchronized static PicLru getInstance() {
        if (instance == null) {
            instance = new PicLru();
        }
        return instance;
    }

    private final int maxMemory = (int) (Runtime.getRuntime().totalMemory()/1024);
    private final int cacheSize = maxMemory/8;
    private LruCache<String,Bitmap> mMemoryCache;



    private void initLru(){
        mMemoryCache = new LruCache<String,Bitmap>(cacheSize){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes()*value.getHeight()/1024;
            }
        };
    }

    public void cachePic(String Url,Bitmap pic){
        String key = Md5Encryption.MD5(Url);

        mMemoryCache.put(key,pic);
    }

    public Bitmap getPic(String Url){
        String key = Md5Encryption.MD5(Url);

        return mMemoryCache.get(key);
    }

}
